package com.longtop.framework.sys.team.web;

import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.Constant;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.team.model.TSysOrgTeam;
import com.longtop.framework.sys.team.model.TSysOrgTeamMx;
import com.longtop.framework.sys.team.service.TeamService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 系统管理-群组
 * 
 * @author SJH
 * 
 */
public class OrgTeamAction extends BaseAction {

	private TeamService service;
	private final String DATASET_ORGTEAM = "Dataset_OrgTeam";
	private final String DATASET_ORGTEAMMX = "Dataset_OrgTeamMx";
	private final String DATASET_TEAMORG = "Dataset_TeamOrg";
	// 查询条件：
	private Map<String, Object> queryMap = new HashMap<String, Object>();

	public OrgTeamAction() {
		this.service = this.getBean(TeamService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {

		String teamId = this.getParameter(context, "orgTeamId");

		// 群组主表
		if (StringUtil.isEmpty(teamId)) {
			Dataset teamDataset = this.getDataset(context, DATASET_ORGTEAM);
			if (!WebUtil.validateDatasetExist(this, teamDataset, DATASET_ORGTEAM)) {
				return;
			}
			// 读取主表：权限控制
			SessionObject sessionObject = this.getSessionObject(context);
			service.getTeamByAuth(sessionObject, teamDataset, TSysOrgTeam.class);
		}
		// 群组子表
		else {
			if (!StringUtil.isEmpty(teamId)) {
				Dataset teamMxDataset = this.getDataset(context, DATASET_ORGTEAMMX);
				if (!WebUtil.validateDatasetExist(this, teamMxDataset, DATASET_ORGTEAMMX)) {
					return;
				}

				queryMap.put("orgTeamId", teamId);
				service.queryForPageByIbatis("sysTeamSql.selectOrgTeamMx", null, queryMap, teamMxDataset, false);
			}
		}
	}

	/**
	 * 提交保存群组
	 * 
	 * @param context
	 */
	public void saveTeam(IActionContext context) {
		Dataset teamDataset = this.getDataset(context, DATASET_ORGTEAM);
		if (!WebUtil.validateDatasetChange(this, teamDataset, DATASET_ORGTEAM)) {
			return;
		}
		try {
			SessionObject sessionObject = this.getSessionObject(context);
			Map changeMap = WebUtil.datasetToObjectList(teamDataset, "orgTeamId", TSysOrgTeam.class);

			Integer[] count = service.saveTeam(changeMap, TSysOrgTeam.class, sessionObject);

			String msg = WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue());
			this.hintInfoMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存失败。");
		}
	}

	/**
	 * 提交保存群组明细
	 * 
	 * @param context
	 */
	public void saveTeamMx(IActionContext context) {
		Dataset teamDataset = this.getDataset(context, DATASET_ORGTEAMMX);
		if (!WebUtil.validateDatasetChange(this, teamDataset, DATASET_ORGTEAMMX)) {
			return;
		}
		try {
			Map changeMap = WebUtil.datasetToObjectList(teamDataset, "id", TSysOrgTeamMx.class);

			Integer[] count = service.saveTeamMx(changeMap, TSysOrgTeamMx.class);

			String msg = WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue());
			this.hintInfoMessage(msg);
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存失败。");
		}
	}

	/**
	 * 查询群组数据
	 * 
	 * @param context
	 */
	private void commonQueryTeam(IActionContext context, boolean isFirst) {
		Dataset teamDataset = this.getDataset(context, DATASET_ORGTEAM);
		if (isFirst) {
			teamDataset.setPageNo(1);
		}
		// 权限控制：只允许显示本机构创建的群组（admin除外）
		SessionObject sessionObject = this.getSessionObject(context);
		QueryConditions conditions = WebUtil.createQueryConditions();
		if (!sessionObject.getUserId().equals(Constant.ADMIN)) {
			conditions.setConditionEqual("createOrgId", sessionObject.getOrgId());
		}

		String teamName = StringUtil.getString(context.getElementValue("Text_orgTeamName"));
		if (!StringUtil.isEmpty(teamName)) {
			conditions.setConditionInclude("orgTeamName", teamName);
		}

		QueryResult queryResult = service.loadByConditions(conditions, teamDataset.getPageNo(), teamDataset.getPageSize(), TSysOrgTeam.class);
		BeanUtil.queryResultToDataset(queryResult, teamDataset, false, true);
	}

	public void queryTeam(IActionContext context) {
		commonQueryTeam(context, true);
	}

	public void loadTeam(IActionContext context) {
		commonQueryTeam(context, false);
	}

	/**
	 * 查询群组明细数据
	 * 
	 * @param context
	 */
	private void commonQueryTeamMx(IActionContext context, boolean isFirst) {
		String teamId = this.getParameter(context, "orgTeamId");

		if (!StringUtil.isEmpty(teamId)) {
			Dataset teamMxDataset = this.getDataset(context, DATASET_ORGTEAMMX);
			if (isFirst) {
				teamMxDataset.setPageNo(1);
			}

			queryMap.put("orgTeamId", teamId);
			service.queryForPageByIbatis("sysTeamSql.selectOrgTeamMx", null, queryMap, teamMxDataset, false);
		}
	}

	public void queryTeamMx(IActionContext context) {
		commonQueryTeamMx(context, true);
	}

	public void loadTeamMx(IActionContext context) {
		commonQueryTeamMx(context, false);
	}

	/**
	 * 加载群组
	 * 
	 * @param context
	 */
	public void getTeam(IActionContext context) {
		Dataset teamDataset = this.getDataset(context, DATASET_TEAMORG);
		if (!WebUtil.validateDatasetExist(this, teamDataset, DATASET_TEAMORG)) {
			return;
		}
		// 读取主表：权限控制
		TeamService teamService = this.getBean(TeamService.BEAN_ID);
		SessionObject sessionObject = this.getSessionObject(context);
		teamService.getTeamByAuth(sessionObject, teamDataset, TSysOrgTeam.class);
	}

}